/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI.Tree;

import Common.IProjectModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author Администратор
 */
public class ProjectModelTree extends JTree {

    private IProjectModel projectModel;

    public void SetModel(IProjectModel projectModel) {
        this.removeAll();
        this.projectModel = projectModel;

        this.setModel(new DefaultTreeModel(new RootNode("Projects", this.projectModel, this)));
        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                switch (e.getButton()) {
                    case MouseEvent.BUTTON3:
                        mouseRightClicked(e);
                }
            }
        });
    }

    private void mouseRightClicked(MouseEvent e) {
        TreePath path = ((JTree) e.getSource()).getClosestPathForLocation(e.getX(), e.getY());
        this.setSelectionPath(path);
        Object component = path.getLastPathComponent();
        if (component instanceof ITree) {
            ((ITree) component).getPopupMenu().show(this, e.getX(), e.getY());
        }
    }
}

